Fix pre-auth double-free in JSON request parser#539
Open
gigioneggiando wants to merge 1 commit into
Open
Conversation
json_string() returns false on a non-string or malformed value without
writing *out. Several callers reparse in place with
`free(x); json_string(&p, &x)` -- the "model" field in
parse_chat_request / parse_completion_request / parse_anthropic_request,
and duplicate JSON keys for type/name/description across the tool-schema
and message parsers. On a failed reparse, *out keeps the just-freed
pointer; the request's cleanup (request_free) then frees it again.
This is reachable pre-auth with a single request, e.g.
POST /v1/chat/completions {"messages":[...],"model":0}
Initialize *out = NULL at the top of json_string so every failure path
leaves the caller's pointer defined, closing all the free-then-reparse
sites at the root rather than patching each call site. The success path
is unchanged (*out is still set by buf_take).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
json_string()returnsfalseon a non-string or malformed value without writing*out. Several callers reparse in place withfree(x); json_string(&p, &x):"model"field inparse_chat_request,parse_completion_request, andparse_anthropic_request;type/name/description/order.nameacross the tool-schema and message parsers (~12 sites in total).On a failed reparse,
*outkeeps the just-freed pointer (dangling, notNULL). The request's cleanup path (request_free) thenfree()s it a second time — a double-free of a small heap allocation, reachable pre-auth with a single request:(also via
/v1/messages,/v1/responses,/v1/completions; and a duplicate-key variant{"content":"ok","content":0}).Fix
Initialize
*out = NULLat the top ofjson_string, so every failure path leaves the caller's pointer defined. This closes all thefree(x); json_string(&p, &x)sites at the root instead of patching each call site, and matches the pattern the parser already uses where it nulls after freeing. Behavior on the success path is unchanged (*outis still overwritten bybuf_take), so valid requests parse exactly as before.Verification
Built the parser under AddressSanitizer/UBSan (driving the real
parse_chat_request, unmodified):AddressSanitizer: attempting double-free— alloc inxstrdup, first free at the"model"handler, second free inrequest_freevia thebad:label.invalid JSON request), no sanitizer error.Found during a coordinated security review of ds4; a standalone offline PoC is available on request. Happy to adjust the approach (e.g. per-call-site nulling) if you prefer.